let Api = require('../../utils/api.js');
let wxRequest = require('../../utils/wxRequest.js');

Page({
  data: {
    username: '',
    password: '',
  },
  onLoad: function () {

  },
  inputusername: function (e) {
    let value = e.detail.value
    this.setData({
      username: value
    })
  },
  inputpassword: function (e) {
    let value = e.detail.value
    this.setData({
      password: value
    })
  },
  logintap: function () {
    let self = this
    wx.showLoading({
      title: '正在登录...',
    })
    wxRequest.getRequest(Api.login(self.data.username, self.data.password), null).then(res => {
      if (res.data) {
        if (res.data.length > 0) {
          let user = res.data[0]
          wx.setStorageSync('user', user)
          wx.showModal({
            title: '提示',
            content: '登录成功！',
            success(res) {
              if (res.confirm) {
                wx.reLaunch({
                  url: '../index/index',
                })
              }
            }
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '用户名不存在或密码错误！',
          })
        }
      } else {
        wx.showModal({
          title: '提示',
          content: '登录失败',
        })
      }
    }).finally(fina => {
      wx.hideLoading()
    })
  },
  registertap: function () {
    let self = this
    wx.showLoading({
      title: '正在注册...',
    })
    wxRequest.getRequest(Api.register(self.data.username, self.data.password), null).then(res => {
      if (res.data) {
        if (res.data == 1) {
          wx.showModal({
            title: '提示',
            content: '注册成功',
          })
          self.setData({
            username: '',
            password: ''
          })
        }
      } else {
        wx.showModal({
          title: '提示',
          content: '注册失败',
        })
      }
    }).finally(fina => {
      wx.hideLoading()
    })
  }
})